<?php

class module_searchuser extends module_base
{
    public function __construct()
    {
        parent::__construct();
        $this->_data_cell = new data_fy_cell();
        $this->_data_building = new data_fy_building();
    }
    /**
     * @author shanghui<shanghui@leju.sina.com.cn>
     * @param $fy_id
     * 根据cell_id获取楼栋信息
     */
    public function get_info_by_id( $cell_id )
    {
        $cell_info = $this->_data_cell->fetch_by_pid( $cell_id );
        if( $cell_info === false )
        {
            //没有取到房源信息
            $this->_fail_result['reason'] = '单元不存在';
            return $this->_fail_result;
        }
        $this->_succ_result['info'] = $cell_info;
        return $this->_succ_result;
    }
    /**
     * 根据楼栋ID 得到单元
     */ 
    public function fetch_by_bid($bid)
    {
        $w = " building_id='{$bid}'";
        return $this->_data_cell->fetch_all_by_condition($w);
    }
    /**
     * 
     * 得到查询条件
     * @param $para
     */
    public function fetch_where($para)
    {
       $where = '';
       
       if (isset($para['project_id'])&& !empty($para['project_id']))
       {
           $where .= " AND project_id = '{$para['project_id']}'";
       }
       
       if (isset($para['building_id'])&& !empty($para['building_id']))
       {
           $where .= " AND building_id = '{$para['building_id']}'";
       }
       return $where;
    }
    /**
     * 
     * 更新
     * @param  $para
     */
    public function update($para)
    {
    	$info = array();
    	if(isset($para['name']))
    	{
    		$info['name'] = $para['name'];
    	}
    	
        if(isset($para['floor_num']))
        {
            $arr = array();
            for($i=$para['floor_num'] ; $i>=1; $i--)
            {
                $arr[$i] = $para['f'.$i];
            }
          
            $info['house_number'] = serialize($arr);
        }
        if(empty($info)) return false;  
        $result = $this->_data_cell->update_by_id($para['id'],$info);
        return $result;
    }
    
    /**
     * 
     * 获取所有单元名称
     */
    public function fetch_all_cell_info_by_bid( $building_id )
    {
        $condition = " building_id = '{$building_id}' ";
        $return = array();
        $rows = $this->_data_cell->fetch_all_by_condition( $condition );
        if ( is_array( $rows ) )
        {
            
            foreach ( $rows as $row ) 
            {
                $return[$row['id']] = $row;
            }
        }
        return $return;
    }
    /**
     * 
     * 获取所有单元名称
     */
    public function fetch_all_cell_info_by_pid( $project_id )
    {
        $condition = " project_id = '{$project_id}' ";
        $return = array();
        $rows = $this->_data_cell->fetch_all_by_condition( $condition );
        if ( is_array( $rows ) )
        {
            
            foreach ( $rows as $row ) 
            {
                $return[$row['id']] = $row;
            }
        }
        return $return;
    }
}
